package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.message.service.MessageService;
import com.xbongbong.saas.service.AIReceiveMessageService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Description: AI及时消息推送相关接口
 * @Author: jiping.yang
 * @Date: 2022-09-23  10:24
 * @version: v1.0
 **/
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/message/mq/ai")
public class MessageRebbitMqAIController {

    @Resource
    private MessageService messageService;
    @Resource
    private AIReceiveMessageService aiReceiveMessageService;

    /**
     * Description: 数据训练消息接口
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @return java.lang.String
     * @author jiping.yang
     * @date 2022/9/26 16:34
     * @since v1.0
     */
    @RequestMapping(value = "/importData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String importData(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.DATA_IMPORT);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 数据训练消息接口
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @return java.lang.String
     * @author jiping.yang
     * @date 2022/9/26 16:34
     * @since v1.0
     */
    @RequestMapping(value = "/trainData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String trainData(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<MessageRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                messageService.delayPushDingtalkOAMessage(messageRabbitMqDTO, PushTypeEnum.TRAIN_MODEL);
                MessageRabbitMqVO messageRabbitMqVO = new MessageRabbitMqVO();
                response = new XbbResponse<>(messageRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 接收AI的消息，处理后进行转发
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @return java.lang.String
     * @author jiping.yang
     * @date 2022/9/26 16:34
     * @since v1.0
     */
    @RequestMapping(value = "/receiveMessage/import", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String importMessage(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String result = aiReceiveMessageService.receiveMessage(messageRabbitMqDTO, PushTypeEnum.DATA_IMPORT);
                response = new XbbResponse<>(result);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 接收AI的消息，处理后进行转发
     * @param messageRabbitMqDTO 消息推送  走消息队列，用于传输数据的
     * @return java.lang.String
     * @author jiping.yang
     * @date 2022/9/26 16:34
     * @since v1.0
     */
    @RequestMapping(value = "/receiveMessage/train", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String trainMessage(@RequestBody @Valid MessageRabbitMqDTO messageRabbitMqDTO, BindingResult br) throws Exception{
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String result = aiReceiveMessageService.receiveMessage(messageRabbitMqDTO, PushTypeEnum.TRAIN_MODEL);
                response = new XbbResponse<>(result);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
